{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "07f36bd5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:hitch-hiker\n",
      "a was found 0 time(s).\n",
      "e was found 1 time(s).\n",
      "i was found 2 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "\n",
    "found = {}  # 创建一个空字典\n",
    "\n",
    "found['a'] = 0    # 将与键（各个元音）关联的值初始化为0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] +=1\n",
    "        \n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,'was found',v,'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fbfcb042",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:hitch-hiker\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'i'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Input \u001b[1;32mIn [2]\u001b[0m, in \u001b[0;36m<cell line: 6>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m letter \u001b[38;5;129;01min\u001b[39;00m word:\n\u001b[0;32m      7\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m letter \u001b[38;5;129;01min\u001b[39;00m vowels:\n\u001b[1;32m----> 8\u001b[0m         found[letter] \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m\n\u001b[0;32m     10\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m k,v \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28msorted\u001b[39m(found\u001b[38;5;241m.\u001b[39mitems()):\n\u001b[0;32m     11\u001b[0m     \u001b[38;5;28mprint\u001b[39m(k,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mwas found\u001b[39m\u001b[38;5;124m'\u001b[39m,v,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtime(s).\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[1;31mKeyError\u001b[0m: 'i'"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "\n",
    "found = {}  \n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] +=1\n",
    "        \n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,'was found',v,'time(s).') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0a4a22fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:apple\n",
      "a was found 1 time(s).\n",
      "e was found 1 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "\n",
    "found = {}  \n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found.setdefault(letter,0)  # 不用再提前初始化字典中的所有数据行\n",
    "        found[letter] +=1\n",
    "        \n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,'was found',v,'time(s).')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3536f37e",
   "metadata": {},
   "source": [
    "## 集合方法\n",
    "### 1. set函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9f4164c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = set('aeeiouu')\n",
    "vowels"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e4ac8b5",
   "metadata": {},
   "source": [
    "## 2. union合并集合（并集）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4870fae7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'h', 'i', 'l', 'o', 'u'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = 'hello'\n",
    "u = vowels.union(set(word))\n",
    "u"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6671fe0",
   "metadata": {},
   "source": [
    "## 3、difference指出共有元素（差集）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bc7e919d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'i', 'u'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = vowels.difference(set(word))\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "28bf991c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'h', 'l'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = set(word).difference(vowels)\n",
    "d\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6889f064",
   "metadata": {},
   "source": [
    "## 4、intersection报告共同对象（交集）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d60a7d45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'o'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i = vowels.intersection(set(word))\n",
    "i"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dae568b9",
   "metadata": {},
   "source": [
    "## 利用python的集合数据结构简化基于列表的代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7df00539",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:apple\n",
      "a\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "# 1.0\n",
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = [] \n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e35a6aae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:apple\n",
      "e\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')   # 用 set函数 创建元音集合\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word)) # intersection 取一个集合的对象，与另一个集合中的对象进行比较，报告找到的共同对象\n",
    "for vowels in found:\n",
    "    print(vowels)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67575c33",
   "metadata": {},
   "source": [
    "## 列表和元组的比较\n",
    "\n",
    "### 1、元组用小括号包围，而列表使用中括号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "63870ffd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels1 = ['a','e','i','o','u']\n",
    "type(vowels1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a88ff600",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels2 = ('a','e','i','o','u')  # 元组用小括号包围\n",
    "type(vowels2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "798a4c4d",
   "metadata": {},
   "source": [
    "### 2.元组是不可变的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2bcf95bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'I', 'o', 'u']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表可变\n",
    "vowels1[2] = 'I'\n",
    "vowels1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "77b3f94f",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [15]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 元组不可变\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m vowels2[\u001b[38;5;241m2\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mI\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m      3\u001b[0m vowels2\n",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "# 元组不可变\n",
    "vowels2[2] = 'I'\n",
    "vowels2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a1472194",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('a', 'e', 'i', 'o', 'u')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "5fc763de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Python'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = ('Python')\n",
    "print(type(t))\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9fd16b48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "('Python',)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2 = ('Python',)\n",
    "print(type(t2))\n",
    "t2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acb2911c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
